package com.vipsoft.base.core;


import com.baomidou.mybatisplus.core.metadata.IPage;

import java.io.Serializable;

public class ApiResult implements Serializable {

    /**
     * 返回编码 0:失败、1：成功
     */
    private int code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回对象
     */
    private Object data;


    public ApiResult() {
        this.code = 1;
        this.message = "请求成功";
    }

    public ApiResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public ApiResult(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.setData(data);
    }

    public ApiResult(boolean flag) {
        this.code = flag ? 1 : -1;
        this.message = flag ? "操作成功" : "操作失败";
        this.setData(data);
    }

    public ApiResult(Object data) {
        this.code = 1;
        this.message = "请求成功";
        this.setData(data);
    }

    public ApiResult(IPage page) {
        this.code = 1;
        this.message = "请求成功";
        PageParam param = new PageParam();
        param.setTotalCount((int) page.getTotal());
        param.setPageCount((int) page.getPages());
        param.setPageIndex((int) page.getCurrent());
        param.setPageSize((int) page.getSize());
        param.setList(page.getRecords());
        this.setData(param);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

}


